<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <!-- 左侧内容 -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col>
      <el-row type="flex" justify="end">
        <!-- 1 -->
        <el-col :span="4">{{ treeNode.manager }}</el-col>
        <el-dropdown @command="operateDepts">
          <!-- 内容 -->
          <span>
            操作
            <i class="el-icon-arrow-down" />
          </span>
          <el-dropdown-menu slot="dropdown">
            <!-- 下拉选项 -->
            <el-dropdown-item
              command="add"
              :disabled="!checkPermission('add-dept')"
            >
              添加子部门
            </el-dropdown-item>
            <el-dropdown-item v-if="!isRoot" command="edit">
              编辑部门
            </el-dropdown-item>
            <el-dropdown-item v-if="!isRoot" command="del">
              删除部门
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',
  components: {},
  props: {
    treeNode: {
      required: true,
      type: Object
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  computed: {},
  watch: {},
  created() { },
  mounted() { },
  methods: {
    operateDepts(type) {
      if (type === 'add') {
        // 添加操作
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        // 编辑操作
        this.$emit('editDepts', this.treeNode)
      } else {
        // 删除操作
        // alert('删除')
        this.$confirm('是否要删除该部门').then(() => {
          // 点击确定就会进入then
          return delDepartments(this.treeNode.id) // 返回一个promise对象 方便进行下一步操作
        }).then(() => {
          // 调用自定义方法 更新数据
          this.$emit('delDepts')
          this.$message('删除部门成功')
        })
      }
    }
  }
}
</script>
<style lang="css" scoped></style>
